package com.bjpowernode.test.servlet;

import com.bjpowernode.test.entity.Emp;
import com.bjpowernode.test.service.EmpService;
import com.bjpowernode.test.service.EmpServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "emp",urlPatterns = "/emp")
public class EmpServlet extends HttpServlet {

    private EmpService empService = new EmpServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1、设置字符集
        req.setCharacterEncoding("UTF-8");
        //2、获取method参数
        String method = req.getParameter("method");
        //3、根据不同的method返回参数调用不同的方法
        if(method.equals("mainUI")){
            this.mainUI(req,resp);
        }else if(method.equals("addUI")){
            this.addUI(req,resp);
        }else if(method.equals("add")){
            this.add(req,resp);
        }else if(method.equals("editUI")){
            this.editUI(req,resp);
        }else if(method.equals("edit")){
            this.edit(req,resp);
        }else if(method.equals("delete")) {
            this.delete(req, resp);
        }
    }

    //删除
    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        // 1、获取删除员工的id信息
        String id = req.getParameter("id");
        int eid = 0;
        if(id != null && !id.equals("")){
            eid = Integer.parseInt(id);
        }
        empService.deleteEmpById(eid);
        // 3、删除之后该跳转页面了
        resp.sendRedirect(req.getContextPath() + "/emp?method=mainUI");
    }

    //保存编辑后的员工信息
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取提交的员工信息
        String id = req.getParameter("id");
        int eid = 0;
        if(id != null && !id.equals("")){
            eid = Integer.parseInt(id);
        }
        String e_name = req.getParameter("e_name");
        String e_age = req.getParameter("e_age");
        //年龄字符串类型转int类型
        int age = 0;
        if(e_age != null && !e_age.equals("")){
            age = Integer.parseInt(e_age);
        }
        String e_address = req.getParameter("e_address");
        String e_phone = req.getParameter("e_phone");

        //用Emp封装
        Emp emp = new Emp(eid, e_name, age, e_address, e_phone);
        //调用保存方法
        empService.editEmp(emp);
        //跳转页面
        resp.sendRedirect(req.getContextPath() + "/emp?method=mainUI");
    }

    //编辑员工信息
    private void editUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        // 1、获取员工id
        String id = req.getParameter("id");
        //进行转换
        int eid = 0;
        if(id != null && !id.equals("")){
            eid = Integer.parseInt(id);
        }
        //查询数据库，获取emp对象
        Emp emp = empService.findEmpById(eid);
        //查询到对象数据之后，存入域中
        req.setAttribute("emp",emp);
        // 转发跳转编辑页面
        req.getRequestDispatcher("/edit.jsp").forward(req,resp);

    }

    //添加员工信息
    private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取员工的学生信息
        String e_name = req.getParameter("e_name");
        String e_age = req.getParameter("e_age");
        //年龄字符串类型转int类型
        int age = 0;
        if(e_age != null && !e_age.equals("")){
            age = Integer.parseInt(e_age);
        }
        String e_address = req.getParameter("e_address");
        String e_phone = req.getParameter("e_phone");

        //用Emp封装
        Emp emp = new Emp(0, e_name, age, e_address, e_phone);
        //调用保存方法
        empService.saveEmp(emp);
        //跳转页面
        resp.sendRedirect(req.getContextPath() + "/emp?method=mainUI");
    }

    //跳转添加页面(拉拉阿拉啦)
    private void addUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.getRequestDispatcher("/add.jsp").forward(req,resp);
    }

    //查询所有员工信息数据
    private void mainUI(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //查询所有的学生信息
         List<Emp> emps = empService.findEmps();
        //将学生信息保存在request域中去
        req.setAttribute("emps",emps);
        //先直接跳转到main.jsp界面上去
        req.getRequestDispatcher("/main.jsp").forward(req,resp);
    }
}
